package com.yangguang.huahui.ui.activitys;

import android.content.Intent;

import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.hyphenate.util.EasyUtils;
import com.yangguang.huahui.R;
import com.yangguang.huahui.ui.fragments.ChatFragment;
import com.yangguang.huahui.utils.LogUtil;

public class ChatActivity extends BaseActivity {
    public static ChatActivity activityInstance;
    private ChatFragment chatFragment;
    String toChatUsername;
    private String projectId="";
    private int detail_type;
    @Override
    protected int initLayoutId() {
        return R.layout.activity_chat;
    }

    @Override
    protected void initView() {
        activityInstance = this;
        toChatUsername = getIntent().getExtras().getString("userId");
        projectId = getIntent().getExtras().getString("id");
        detail_type = getIntent().getExtras().getInt("detail_type");
        //use EaseChatFratFragment
        chatFragment = new ChatFragment();
        //pass parameters to chat fragment
        chatFragment.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction().add(R.id.container, chatFragment).commit();
    }

    @Override
    protected void initData() {
        try {
            LogUtil.e(EMClient.getInstance().contactManager().getBlackListFromServer()+"", true);
        } catch (HyphenateException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        // make sure only one chat activity is opened
        String username = intent.getStringExtra("userId");
        if (toChatUsername.equals(username))
            super.onNewIntent(intent);
        else {
            finish();
            startActivity(intent);
        }

    }


    @Override
    public void onBackPressed() {
        chatFragment.onBackPressed();
        if (EasyUtils.isSingleActivity(this)) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        activityInstance = null;
    }
}
